<template>
  <div class="login-container">
    <el-form ref="form" :model="form" label-width="80px" class="login-form" :rules="rules">
      <h2 class="login-title">VIPSystemMamanger</h2>
      <el-form-item label="账号" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <!-- onSubmit(form)中的form为表单的ref的值 -->
        <el-button type="primary" @click="submitForm('form')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
//从api/login.js中导入已经定义好的后台api {login,getUserInfo} 采用映射的方式  ，导入后可直接调用
import { login, getUserInfo } from "@/api/login";
export default {
  data() {
    return {
      form: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "登录名称不能为空", trigger: "blur" },
          { min: 5, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "登录密码不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      //只有表单校验通过了，才可以进行提交
      this.$refs[formName].validate(valid => {
        if (valid) {
          //如果valid为true  即表示所有验证通过  ---提交表单到后台进行验证是否正确
          //调用函数
          login(this.form.username, this.form.password).then(response => {
            console.log(response.data);
            const resp = response.data;
            if (response.flag) {
              //表示验证成功
              //通过token，异步获取用户信息
              getUserInfo(resp.token).then(response => {
                //获取到用户信息
                const userinfo = response;
                if (userinfo.flag) {
                  console.log("userinfo", userinfo.data);
                  //1.将数据保存到localStorage   保存token,用户信息
                  localStorage.setItem("yyy-msm-user",JSON.stringify(userinfo, null, 2));
                  localStorage.setItem("yyy-msm-token", resp.token);
                  //2前往首页
                  this.$router.push("/");
                } else {
                  this.$message({
                    showClose: true,
                    message: userinfo.message,
                    type: "warning"
                  });
                }
              });
            }
          });
        } else {
          //未通过，弹出警告
          console.log(resp.message);
          this.$message({
            showClose: true,
            message: resp.message,
            type: "warning"
          });
          return false;
        }
      });
    }
  }
};
</script>
<style  scoped>
.login-form {
  width: 350px;
  /*上下160px 左右居中 */
  margin: 160px auto;
  background-color: rgb(255, 255, 255, 0.8);
  padding: 30px;
  border-radius: 20px;
}
.login-container {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url("../../assets/login.jpg");
}
.login-title {
  color: #303133;
  text-align: center;
}
</style>